package info.javafans.servlet;

import info.javafans.dao.CategoryDao;
import info.javafans.dao.CommentDao;
import info.javafans.dao.PostDao;
import info.javafans.dao.UserDao;
import info.javafans.domain.Category;
import info.javafans.domain.Comment;
import info.javafans.domain.Post;
import info.javafans.domain.User;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class AdminServlet
 */
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");

		if (action == null) {
			response.sendRedirect("WelcomeServlet");
		}

		if (action.equals("login")) {
			this.doLogin(request, response);
		} else if (action.equals("logout")) {
			this.doLogout(request, response);
		} else if (action.equals("manage")) {
			this.doManage(request, response);
		} else if (action.equals("editPost")) {
			this.doEditPost(request, response);
		} else if (action.equals("saveEdit")) {
			this.doSaveEditsaveEdit(request, response);
		} else if (action.equals("deletePost")) {
			this.doDeletePost(request, response);
		}
	}

	//Login
	private void doLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserDao userDao = new UserDao();
		User user = userDao.UserLogin(username, password);
		if (user != null) {
			request.getSession().setAttribute("user", user);
			response.sendRedirect("WelcomeServlet");
		}
		else {
			request.setAttribute("error", "用户名或密码错误!");
			request.getRequestDispatcher("login.jsp").forward(request,
					response);
		}
	}
	//Logout
	private void doLogout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		request.getRequestDispatcher("WelcomeServlet").forward(request,
				response);
	}
	//Manage
	private void doManage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PostDao postDao = new PostDao();
		CommentDao commentDao = new CommentDao();
		
		//获得用户列表
		List<User> userList = new UserDao().selectAllUsers();
		request.setAttribute("userList", userList);

		//获得文章分类列表--common
		CategoryDao categoryDao = new CategoryDao();
		List<Category> categoryList = categoryDao.selectAllCategory();
		request.setAttribute("categoryList", categoryList);
		
		//获得viewCount排名文章列表--common
		List<Post> viewRankList = postDao.selectMostViewTitle();
		request.setAttribute("viewRankList", viewRankList);
		
		//获得评论列表--common
		List<Comment> newCommentList = commentDao.selectNewComments();
		request.setAttribute("newCommentList", newCommentList);
		
		//获得最新文章标题列表--common
		List<Post> newPostTitleList = postDao.selectNewPostTitle();
		request.setAttribute("newPostTitleList", newPostTitleList);
		
		request.getRequestDispatcher("admin.jsp").forward(request, response);
	}
	//EditPost
	private void doEditPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		
		//获得文章
		PostDao postDao = new PostDao();
		Post post = postDao.selectPostById(id);
		request.setAttribute("post", post);
		
		//获得该文章的评论列表
		CommentDao commentDao = new CommentDao();
		List<Comment> commentList = commentDao.selectCommentsOfPost(id);
		request.setAttribute("commentList", commentList);
		
		//获得文章分类列表--common
		CategoryDao categoryDao = new CategoryDao();
		List<Category> categoryList = categoryDao.selectAllCategory();
		request.setAttribute("categoryList", categoryList);
		
		//获得viewCount排名文章列表--common
		List<Post> viewRankList = postDao.selectMostViewTitle();
		request.setAttribute("viewRankList", viewRankList);
		
		//获得评论列表--common
		List<Comment> newCommentList = commentDao.selectNewComments();
		request.setAttribute("newCommentList", newCommentList);
		
		//获得最新文章标题列表--common
		List<Post> newPostTitleList = postDao.selectNewPostTitle();
		request.setAttribute("newPostTitleList", newPostTitleList);
		
		request.getRequestDispatcher("editPost.jsp").forward(request, response);
	}
	//saveEdit
	private void doSaveEditsaveEdit(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String title = request.getParameter("title");
		String categoryId =  request.getParameter("category");
		String tags = request.getParameter("tags");
		String content = request.getParameter("content");
		
		Post post = new Post();
		post.setId(id);
		Category category = new Category();
		category.setId(categoryId);
		post.setCategory(category);
		
		post.setTitle(title);
		post.setTags(tags);
		post.setContent(content);
		
		new PostDao().updatePost(post);
		response.sendRedirect("ShowSinglePost?id="+id);
	}
	//deletePost
	private void doDeletePost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		
		new PostDao().deletePostById(id);
	}
}
